package com.besttone.locate.util;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.besttone.locate.model.LocateInfoModel;

public class LocateUtil {

	public String getMobileLocateInfo(String phoneNo) throws Exception {
		LocateInfoModel model = new LocateInfoModel();
		model.setAppKey(MobileConfig.appKey);
		model.setApiName(MobileConfig.apiName);
		model.setApiMethod(MobileConfig.apiMethod);
		model.setMobileNo(phoneNo);
		model.setTimestamp(getTimeStamp());
		
		// 生成请求xml
		String reqXml = createParmXml(model);
		
		//返回XML
		String rtnXml = "";
		//http地址
		StringBuilder requestUrl = new StringBuilder(MobileConfig.serverAddress);
		String parm = "?reqXml=" + URLEncoder.encode(reqXml, "UTF-8") + "&sign=" + MD5Builder.getMD5Str(reqXml + MobileConfig.appSecret);
		requestUrl.append(parm);
		String httpUrl = requestUrl.toString();
		System.out.println("请求URL="+httpUrl);
		//HttpGet连接对象
		HttpGet httpRequest = new HttpGet(httpUrl);  
		//取得HttpClient对象  
		HttpClient httpclient = new DefaultHttpClient(); 
		//请求HttpClient，取得HttpResponse  
			HttpResponse httpResponse = httpclient.execute(httpRequest); 
			//请求成功  
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				//取得返回的字符串  
				rtnXml = EntityUtils.toString(httpResponse.getEntity()).trim(); 
				System.out.println(rtnXml);
			}
			return rtnXml;
	}

    public String getTimeStamp() {
    	String timestamp = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		timestamp = sdf.format(new Date());
		return timestamp;
    }

	public String createParmXml(LocateInfoModel model) {
		StringBuffer parmXml = new StringBuffer("");
		parmXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		parmXml.append("<reqXML version=\"1.0\">");
		String appKeyParm = "<appKey>" + model.getAppKey() + "</appKey>";
		String apiNameParm = "<apiName>" + model.getApiName() + "</apiName>";
		String apiMethodParm = "<apiMethod>" + model.getApiMethod() + "</apiMethod>";
		String timestampParm = "<timestamp>" + model.getTimestamp() + "</timestamp>";
		parmXml.append(appKeyParm);
		parmXml.append(apiNameParm);
		parmXml.append(apiMethodParm);
		parmXml.append(timestampParm);
		parmXml.append("<params>");
		String mobileParm = "<param name=\"mobile\" value=\"" + model.getMobileNo() + "\"/>";
		parmXml.append(mobileParm);
		parmXml.append("</params>");
		parmXml.append("</reqXML>");
		String parmXmlStr = parmXml.toString();
		System.out.println("生成的XML=");
		System.out.println(parmXmlStr);
		return parmXmlStr;
	}

    public void main(String args[]) {
        String phone = "18930823570";
        LocateUtil util = new LocateUtil();
        try {
            String rtnXml = util.getMobileLocateInfo(phone);
            System.out.println(rtnXml);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    
}